package com.forcetools.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import org.apache.commons.digester.Digester;

import com.forcetools.digester.EnumPropertySetterRule;

public class MetadataMarshaller {
	private Digester digester;
	public MetadataMarshaller(){
        digester = new Digester();
        digester.setValidating( false );

        digester.addObjectCreate( "CustomObject", com.forcetools.wrappers.CustomObjectWrapper.class);
        digester.addRule( "CustomObject/deploymentStatus", new EnumPropertySetterRule("deploymentStatus"));
        digester.addRule( "CustomObject/sharingModel", new EnumPropertySetterRule("sharingModel"));
        digester.addBeanPropertySetter( "CustomObject/pluralLabel", "pluralLabel" );
        digester.addBeanPropertySetter( "CustomObject/customHelp", "customHelp" );
        digester.addBeanPropertySetter( "CustomObject/customHelpPage", "customHelpPage" );
        digester.addBeanPropertySetter( "CustomObject/description", "description" );
        digester.addObjectCreate( "CustomObject/fields",  com.sforce.soap.metadata.CustomField.class);
        digester.addSetNext("CustomObject/fields","addField");
	}
	
	public com.sforce.soap.metadata.Metadata parseFromXml(String xmlDoc) throws Exception{
		digester.clear();
        return (com.sforce.soap.metadata.Metadata)digester.parse( xmlDoc);
		
	}
	public com.sforce.soap.metadata.Metadata parseFromXml(InputStream xmlStream) throws Exception{
		digester.clear();
        return (com.sforce.soap.metadata.Metadata)digester.parse( xmlStream);
		
	}
	
}
